package com.zeus.hello;

import com.zeus.util.ActivitiUtil;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/8/30.
 * 该类为了演示流程变量而定义
 * 流程变量的作用
 * （1）传递业务数据
 * 如请假天数、请假时间、请假事由
 * （2）指定连线完成任务
 * （3）动态指定办理人
 */
public class ProcessVariable {
    ProcessEngine processEngine = ActivitiUtil.getProcessEngine();

    @Test
    public void deploymentProcessDefinition(){
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .name("流程定义")
                .addClasspathResource("diagrams/processVariables.bpmn")
                .addClasspathResource("diagrams/processVariables.png")
                .deploy();
        //System.out.println(processEngine);
        System.out.println("deploy ID:"+deployment.getId());
        System.out.println("deploy name:"+deployment.getName());
    }

    /**
     * 采用输入流的方式加载流程定义
     * 关于资源读取的路径问题
     * this.getClass().getResourceAsStream("diagrams/processVariables.bpmn")
     * this.getClass().getResourceAsStream("/diagrams/processVariables.bpmn")
     * this.getClass().getClassLoader().getResourceAsStream("diagrams/processVariables.bpmn")
     * 第一种方法是从当前类的package中加载资源文件
     * 第二种和第三种都是从类的根路径来加载资源文件
     *
     */
    @Test
    public void deploymentProcessDefinitionByInputStream(){
        InputStream bpmnIn = this.getClass().getResourceAsStream("/diagrams/processVariables.bpmn");
        InputStream pngIn  = this.getClass().getResourceAsStream("/diagrams/processVariables.png");
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .name("流程变量定义")
                .addInputStream("processVariables.bpmn", bpmnIn)
                .addInputStream("processVariables.png", pngIn)
                .deploy();
        System.out.println("deploy ID:"+deployment.getId());
        System.out.println("deploy name:" + deployment.getName());
    }

    /**
     * 启动流程实例
     * 通过key来启动流程实例系统采用的是最新版本的流程定义
     */
    @Test
    public void startProcessInstance(){
        //以流程定义对象的key来启动ProcessInstance
        //（1）获取RuntimeService
        String processDefinitionKey = "processVariables";
        ProcessInstance processInstance = processEngine.getRuntimeService()
                .startProcessInstanceByKey(processDefinitionKey);
        System.out.println("流程实例ID:" + processInstance.getId());
        System.out.println("流程定义ID:" + processInstance.getProcessDefinitionId());

    }

    /**
     * 设置流程变量
     * 与流程定义相关的表
     * act_ru_variable
     * act_hi_varinst
     */
    @Test
    public void setProcessVariables(){
        String taskId = "20002";
        TaskService taskService = processEngine.getTaskService();
        taskService.setVariable(taskId,"请假天数",3);
        taskService.setVariable(taskId, "请假日期", new Date());
        taskService.setVariable(taskId, "请假原因", "回家探亲");

        /**
         * 设置javabean作为流程变量时必须实现Serializable接口
         * 获取javabBean变量时，如果javabean的属性发生变化（如增加属性），那么将发生异常
         * 可以设置类的serialVersionUID = ....
         * 来消除异常
         */
    }

    /**
     * 获取流程变量的值
     */
    @Test
    public void getProcessVariables(){
        String taskId = "15004";
        TaskService taskService = processEngine.getTaskService();
        Integer days = taskService.getVariable(taskId, "请假天数", Integer.class);
        Date    date = taskService.getVariable(taskId,"请假日期",Date.class);
        String  reason = taskService.getVariable(taskId,"请假原因",String.class);
        System.out.println(days + ":" + date + ":" + reason);

    }

    /**
     * 模拟获取流程变量的场景
     */
    public void setAndGetVariables(){
        /**
         * 获取运行时对象
         */
        RuntimeService runtimeService = processEngine.getRuntimeService();
        /**
         * 获取任务服务对象
         */
        TaskService taskService = processEngine.getTaskService();

        /**
         * 设置流程变量
         * (1)使用执行对象ID来设置流程变量
         * （2）使用任务ID来设置流程变量
         * （3）启动流程实体来设置流程变量
         * （4）完成任务时设置流程变量
         */
        //runtimeService.setVariables(executionId,variables);

        //taskService.setVariables(taskId,variable);

        //runtimeService.startProcessInstanceByKey(processDefinition,variables);

        //taskService.complete(taskId,variables);

        /**
         * 获取流程变量
         * (1)使用执行ID获取流程变量
         */
        //runtimeService.getVariable(executionId,variableName);
    }

    /**
     * 完成任务
     */
    @Test
    public  void completeMyPersonalTask(){
        String taskId = "15004";
        processEngine.getTaskService()
                .complete(taskId);
        System.out.println("完成任务：任务ID:"+taskId);
    }

    /**
     * 查询历史流程变量信息
     */
    @Test
    public void findHistoryProcessVariables(){
        List<HistoricVariableInstance> list = processEngine.getHistoryService()
                .createHistoricVariableInstanceQuery()
                .variableName("请假天数")
                .list();
        //

    }
}
